Skip to content

Convert the KV cache one layer at a time and compress it while prefilling - #849

Open
aleroot wants to merge 1 commit into
ml-explore:mainfrom
aleroot:alessio/estimator-followups
Open

aleroot wants to merge 1 commit into
ml-explore:mainfrom
aleroot:alessio/estimator-followups

Conversation

@aleroot

@aleroot aleroot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Follow-up to #748, for @davidkoski's comment on the compression start and prefill.

A compressed cache converted every layer in one graph, and the runtime kept the full-precision leaves until it returned, so the whole cache existed twice while it converted. A long first prompt also landed in full precision whole, because generation compressed only after the prefill.

  • One layer at a time. Each converted layer is evaluated before the next converts, and the runtime no longer holds the old leaves.
  • Compress while prefilling. New PrefillParameters.cacheCompression. With .whilePrefilling, the default, an affine cache stops the prefill right after its compression start, compresses, and writes the rest of the prompt compressed, like mlx-lm. .afterPrefill keeps the previous behaviour. Prompts with media and the MTP and block diffusion iterators still compress after the prefill.
  • Estimator. peakBytes follows both. New checkpointBytes(tokens:) reports what a ChatSession prompt checkpoint copies (sliding windows, recurrent state, a requested capacity once it evicts), which an app sizing a session against memory missed before.

Measured on an M5 Max, Qwen3-Reranker-0.6B-4bit, 32k tokens:

Before After
Converting, affine (peak) 5.16 GB 4.39 GB
Converting, TurboQuant (peak) 5.51 GB 4.42 GB
Affine prefill (peak over weights) 4.2 GB 2.4 GB, same speed

Costs and limits:

  • On head dim 256 the prompt past the start attends through quantized matmuls, so there it costs memory instead of saving it: +190 MB and 10% slower on Qwen3.8-27B-4bit with an 8k prompt. .afterPrefill turns it off.
  • TurboQuant and variance normalization always compress after the prefill: compressing them while prefilling was 13-18x slower and peaked higher.
  • Variance normalization's conversion peak is unchanged; it is the float32 working memory of one layer, which the estimate still leaves out.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

…ling

Follow-up to ml-explore#748. A compressed cache converted every layer in one graph,
and the runtime kept the full-precision leaves until it returned, so the
whole cache existed twice while it converted. A long first prompt also
landed in full precision whole, because generation compressed only after
the prefill. The estimate described that, but the runtime could do better.

Design

- Each converted layer is evaluated before the next converts, and
  applyKVCacheConfigurationValidated and maybeTurboQuantizeKVCache no
  longer hold the old leaves, so one layer holds both copies at a time.
- PrefillParameters.cacheCompression chooses when an affine cache
  compresses a prompt that crosses its start. .whilePrefilling, the
  default, stops the prefill right after the start, compresses, and writes
  the rest of the prompt compressed, like mlx-lm. It reuses the prefill
  split of prompt checkpoints, in TokenIterator and
  SpeculativeTokenIterator. .afterPrefill keeps the previous behaviour.
  Prompts with media, non-balanced chunking, and the MTP and block
  diffusion iterators compress after the prefill.
- GenerationEstimator follows both: peakBytes converts layers in order,
  and Cache.prefillCompression says when a prompt compresses.
- Layer.checkpoint and checkpointBytes(tokens:) estimate what a
  ChatSession prompt checkpoint copies: sliding windows, recurrent state,
  a requested capacity once it evicts, and an incomplete
  variance-normalized tile. An app sizing a session against memory missed
  them before.

Measured on an M5 Max, release build:

- Converting a 32k cache on Qwen3-Reranker-0.6B-4bit, peak: affine
  5.16 -> 4.39 GB, TurboQuant 5.51 -> 4.42 GB. Variance normalization is
  unchanged at 5.87 GB: its peak is the float32 working memory of one
  layer, which the estimate still leaves out.
- 32k affine prefill on the same model: peak 4.2 -> 2.4 GB above the
  weights, same speed.

Costs and limits

- On head dim 256 (Qwen3.8-27B-4bit, 8k prompt) the prompt past the start
  attends through quantized matmuls: +190 MB peak and 10% slower prefill.
  .afterPrefill turns it off.
- TurboQuant and variance normalization always compress after the
  prefill. Compressing them while prefilling was 13-18x slower and peaked
  higher.

Tests check that the split matches two generations that end at the start,
that both speculative caches compress, that a layer is freed before the
next converts, and that checkpointBytes matches real checkpoints for
sliding windows, recurrent state, a requested capacity and variance
normalization.

AI usage: Filo, an AI coding assistant, wrote the code, tests and this
message, and ran the unit tests, the DocC check and the measurements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants